package com.shop.cereshop.app.service.msg.impl;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.cereshop.app.dao.collect.CereBuyerCollectDAO;
import com.shop.cereshop.app.dao.notice.CereSystemUserNoticeMapper;
import com.shop.cereshop.app.dao.recommend.CereRecommendCommentDAO;
import com.shop.cereshop.app.dao.recommend.CereRecommendLikesDAO;
import com.shop.cereshop.app.domain.msg.UnreadMsgCountVO;
import com.shop.cereshop.app.domain.notice.SystemNoticeVO;
import com.shop.cereshop.app.page.recommend.InteractiveMsgVO;
import com.shop.cereshop.app.param.sysmsg.ReadAllMsgParam;
import com.shop.cereshop.app.service.msg.SysMsgService;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.tencent.im.enums.notice.SysMsgTypeEnum;
import com.shop.cereshop.commons.enums.InteractiveMsgTypeEnum;
import com.shop.cereshop.commons.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 消息接口
 * @author zdd
 * @date 2025/10/13 09:36
 * @version 1.0
 */
@Slf4j
@Service
public class SysMsgServiceImpl implements SysMsgService {

    @Resource
    private CereRecommendLikesDAO recommendLikesDAO;
    @Resource
    private CereBuyerCollectDAO buyerCollectDAO;
    @Resource
    private CereRecommendCommentDAO recommendCommentDAO;
    @Resource
    private CereSystemUserNoticeMapper systemUserNoticeMapper;


    @Override
    public Result<UnreadMsgCountVO> getUnreadMsgCount(Long userId) {
        // 系统消息
        Long sysMsgCount = systemUserNoticeMapper.getUserUnreadNoticeCount(userId);

        // 互动消息
        Long interactiveMsgCount = recommendCommentDAO.getUnreadCommentCount(userId);

        // 点赞和收藏
        Long unreadLikeCount = recommendLikesDAO.findUserUnreadLikeCount(userId);
        Long collectCount = buyerCollectDAO.findUserCollectCount(userId);
        Long likeAndCollectCount = unreadLikeCount + collectCount;

        UnreadMsgCountVO countVO = new UnreadMsgCountVO();
        countVO.setSysMsgCount(sysMsgCount);
        countVO.setInteractiveMsgCount(interactiveMsgCount);
        countVO.setLikeAndCollectCount(likeAndCollectCount);
        return Result.success(countVO);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<Boolean> setReadAllMsg(Long buyerUserId, ReadAllMsgParam param) {
        Integer type = param.getType();
        if (type == null) {
            return Result.failed("请选择消息类型");
        }
        if(type < 1 || type > 3){
            return Result.failed("不支持的类型: " + type);
        }
        if (SysMsgTypeEnum.SYSTEM_MSG.getCode().equals(type)) {
            systemUserNoticeMapper.updateUserNoticeIsRead(buyerUserId, null);
            return Result.success();
        }
        if (SysMsgTypeEnum.INTERACTIVE_MSG.getCode().equals(type)) {
            recommendCommentDAO.markCommentsOnMyTrendsAsRead(buyerUserId);
            recommendCommentDAO.markRepliesToMyCommentsAsRead(buyerUserId);
            return Result.success();
        }
        if (SysMsgTypeEnum.LIKE_AND_COLLECT.getCode().equals(type)) {
            recommendLikesDAO.updateCommentLikeIsReadByUserId(buyerUserId);
            buyerCollectDAO.updateCollectIsReadByUserId(buyerUserId);
            return Result.success();
        }
        return Result.failed("设置已读状态失败，请稍后重试");
    }

    @Override
    public Result<Page<SystemNoticeVO>> getSystemNoticePage(Integer page, Integer pageSize, Long buyerUserId) {
        PageHelper.startPage(page, pageSize);
        List<SystemNoticeVO> noticePage = systemUserNoticeMapper.getSystemNoticePage(buyerUserId);
        PageInfo<SystemNoticeVO> pageInfo = new PageInfo<>(noticePage);
        Page<SystemNoticeVO> result = new Page(pageInfo.getList(), pageInfo.getTotal());
        return Result.success(result);
    }

    @Override
    public Result<Page<InteractiveMsgVO>> getInteractiveMsgPage(Integer page, Integer pageSize, Long buyerUserId) {
        PageHelper.startPage(page, pageSize);
        List<InteractiveMsgVO> msgPageList = recommendCommentDAO.getInteractiveMsgPage(buyerUserId,null, null);
        if (CollUtil.isNotEmpty(msgPageList)) {
            msgPageList.forEach(msg -> msg.setMsgTypeDesc(InteractiveMsgTypeEnum.getMsgByCode(msg.getMsgType())));
        }
        PageInfo<InteractiveMsgVO> pageInfo = new PageInfo<>(msgPageList);
        Page<InteractiveMsgVO> result = new Page(pageInfo.getList(), pageInfo.getTotal());
        return Result.success(result);
    }


}
